home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 4 / United Public Domain Gold 4.iso / scope / sc093.dms / sc093.adf / LM / lm3.c < prev    next >
C/C++ Source or Header  |  1989-12-02  |  6KB  |  327 lines

  1. #include    <stdio.h>
  2. #include    "lm.h"
  3.  
  4. extern    long    in_file, out_file;
  5. extern  char    *out_file_name, *list_file_name;
  6. struct    symbol    *sym_list_head;
  7. struct    fname    *mod_list_head;
  8. char        *module_name;
  9. extern     long    mod_buf[], DeleteFile();
  10. extern    int    name_count;        /* number of user-supplied names */
  11.  
  12. struct fname     *compare_symbol_names();
  13. long        Open(), Write(), Output();
  14. FILE        *list_file;
  15. int        print_num = 0;
  16. char        name_buf[33];
  17.  
  18.  
  19. /* --------------------------------------------------------------------------
  20.             DELETE_MODULE ROUTINES
  21. ---------------------------------------------------------------------------*/
  22.  
  23. delete_modules( file_list )
  24. char    *file_list;
  25.     {
  26.     int        size;
  27.     struct fname    *p;
  28.  
  29.     create_name_list( file_list );    /* get file list */
  30.  
  31.     while( (size = read_lib_module() ) )
  32.         {
  33.         if( (p = compare_symbol_names() ) )
  34.             {
  35.             name_count--;
  36.             printf( ">> Deleting module: <%s>\n", p->name );
  37.  
  38.             *p->name = '\0';    /* kill the name */
  39.             }
  40.         else
  41.             Write( out_file, mod_buf, (long) (size << 2) );
  42.         }
  43.  
  44.     if ( name_count )    /* some no-matches found */
  45.         print_nfound_modules();
  46.     }
  47.  
  48.  
  49.  
  50. /* --------------------------------------------------------------------------
  51.             REPLACE_MODULE ROUTINES
  52. ---------------------------------------------------------------------------*/
  53.  
  54. replace_modules( file_list)
  55. char    *file_list;
  56.     {
  57.     int        size;
  58.     struct fname    *p;
  59.     long        longword, fp;
  60.  
  61.  
  62.     create_name_list( file_list );    /* get file list */
  63.     
  64.     while( (size = read_lib_module() ) )
  65.         {
  66.         if( (p = compare_symbol_names() ) )
  67.             {
  68.             /* 
  69.               Attempt to open the file, read it in, and write it
  70.               out to the output file.
  71.             */
  72.  
  73.             name_count--;
  74.  
  75.             if( !(fp = Open( p->filename, MODE_OLDFILE ) ) )
  76.                 {
  77.                 printf( "ERROR: cannot open: <%s>\n", p->filename );
  78.                 }
  79.             else
  80.                 {
  81.                 printf( ">> Replacing: <%s>\n", p->name );
  82.                 
  83.                 copy_lib( fp, out_file );
  84.                 Close( fp );
  85.                 }
  86.  
  87.             *p->name = '\0';
  88.             }
  89.  
  90.         else
  91.             Write( out_file, mod_buf, (long) (size << 2) );
  92.         }
  93.  
  94.     if ( name_count )    /* some no-matches found */
  95.         print_nfound_modules();
  96.     }
  97.  
  98.  
  99. /* --------------------------------------------------------------------------
  100.             EXTRACT_MODULE_ROUTINES
  101. ---------------------------------------------------------------------------*/
  102.  
  103. extract_modules( file_list )
  104. char    *file_list;
  105.     {
  106.     int        size;
  107.     struct fname    *p;
  108.  
  109.  
  110.     create_name_list( file_list );    /* get file list */
  111.     
  112.     while( (size = read_lib_module() ) )
  113.         {
  114.         if( !name_count )    /* all done? */
  115.             break;
  116.  
  117.         if( (p = compare_symbol_names() ) )
  118.             {
  119.             name_count--;
  120.             dump_module( p->name, size );
  121.             *p->name = '\0';
  122.             }
  123.         }
  124.  
  125.     if ( name_count )    /* some no-matches found */
  126.         print_nfound_modules();
  127.     }
  128.  
  129.  
  130.  
  131.  
  132. /* --------------------------------------------------------------------------
  133.             INTERACTIVE ROUTINES
  134. ---------------------------------------------------------------------------*/
  135.  
  136. do_interactive_stuff()
  137.     {
  138.     int    size;
  139.     char    choice;
  140.     
  141.     while( (size = read_lib_module() ) )
  142.         {
  143.         print_modules();
  144.         printf( "\nChoice: Delete, Extract, Next, Quit, Abort " );
  145.         
  146.         choice = tolower( getchar() );
  147.         getchar();
  148.         printf( "\n" );
  149.  
  150.         if ( choice == 'd' )
  151.             {
  152.             printf( ">> Deleting module: <%s>\n", module_name );
  153.             continue;
  154.             }
  155.             
  156.         if ( choice == 'e' )
  157.             {
  158.             dump_module( module_name, size );
  159.             }
  160.  
  161.         if ( choice == 'q' )
  162.             {
  163.             return;            
  164.             }
  165.  
  166.         if ( choice == 'a' )
  167.             {
  168.             my_exit( EABORT );
  169.             }
  170.  
  171.         if ( choice == 'n' )
  172.             {
  173.             }
  174.  
  175.         Write( out_file, mod_buf, (long) (size << 2) );
  176.         }
  177.     }
  178.  
  179.  
  180.  
  181. print_modules()
  182.     {
  183.     struct symbol    *p = sym_list_head->next;
  184.     
  185.     printf( "\nModules in this section: \n" );
  186.  
  187.     /*
  188.       We will have at least one name in the section. We pick the first
  189.       one for module_name.
  190.     */
  191.     
  192.     module_name = p->name;
  193.  
  194.     for ( ; p ; p = p->next )
  195.         {
  196.         printf( "\t%s\n", p->name );
  197.         }
  198.     }
  199.     
  200.  
  201. dump_module( mod_name, module_size )
  202. char    *mod_name;
  203. int    module_size;
  204.     {
  205.  
  206.     long    fp;
  207.     char    file_name[34];
  208.  
  209.     /*
  210.       Add an '.obj', extension to the module name.
  211.     */
  212.     
  213.     strcpy( file_name, mod_name );
  214.     strcat( file_name, ".obj" );
  215.  
  216.     printf( ">> Extracting: <%s> to: <%s>\n", mod_name, file_name );
  217.  
  218.     if( !(fp = Open( file_name, MODE_NEWFILE ) ) )
  219.         {
  220.         printf( "ERROR: Unable to extract: <%s>\n", mod_name );
  221.         return( FALSE );
  222.         }
  223.  
  224.     Write( fp, mod_buf, (long) (module_size << 2) );
  225.     Close( fp );
  226.     }
  227.  
  228.  
  229. struct fname     *compare_symbol_names()
  230.     {
  231.     /*
  232.       Hop down the list of user supplied names and see if any of them
  233.       match any of the names in the module list.
  234.     */
  235.  
  236.     struct symbol    *p;
  237.     struct fname    *q;
  238.  
  239.     for( q = mod_list_head; q ; q = q->next )
  240.         {
  241.         for ( p = sym_list_head->next; p ; p = p->next )
  242.             {
  243.             if ( !strcmp( p->name, q->name ) )
  244.                 return( q );
  245.             }
  246.         }
  247.         
  248.     return( NULL );
  249.     
  250.     
  251.     }
  252.  
  253.  
  254. print_nfound_modules()
  255.     {
  256.     /* 
  257.       Print the list of modules that are not found. If we found the
  258.       module and performed the desired operation, the first char of
  259.       the name will be '\0'.
  260.     */
  261.     
  262.     struct fname    *p;
  263.     
  264.     printf( "WARNING: The following modules were not found: \n" );
  265.  
  266.     for( p = mod_list_head; p ; p = p->next )
  267.         {
  268.         if ( *p->name )
  269.             printf( "\t%s\n", p->name );
  270.         }
  271.     }
  272.  
  273.  
  274. /* --------------------------------------------------------------------------
  275.                 LIST_LIBRARY ROUTINES
  276. ---------------------------------------------------------------------------*/
  277.  
  278. list_library( list_file_name )
  279. char    *list_file_name;
  280.  
  281.     {
  282.     struct     symbol    *p;
  283.     
  284.     /* 
  285.       If the first char of list_file_name is null, use the 
  286.       stdio device for output, else attempt to open the 
  287.       supplied file name.
  288.     */
  289.  
  290.     list_file = (*list_file_name) ? fopen( list_file_name, "w+" ) : stdout;
  291.  
  292.     if ( !list_file )
  293.         {
  294.         my_exit( ENOLIST, list_file_name );
  295.         }
  296.  
  297.     printf( ">> Listing library to: <%s>\n", 
  298.         (*list_file_name) ? list_file_name : "CON" );
  299.  
  300.     
  301.     while( read_lib_module() )
  302.         {
  303.         /*
  304.           Print the external symbols in the symbol list.
  305.         */
  306.         for( p = sym_list_head->next; p; p = p->next )
  307.             fprintf( list_file, (++print_num % 2) ? 
  308.                  "  %-44s" : "  %s\n", p->name );
  309.         }
  310.  
  311.  
  312.     /*
  313.       If we print an odd number of files, we must also print 
  314.       a newline character.
  315.     */
  316.  
  317.     if ( print_num % 2 )
  318.         fprintf( list_file, "\n" );
  319.  
  320.     if ( *list_file_name ) 
  321.         fclose( list_file );
  322.         
  323.     my_exit( 0 );
  324.     }
  325.  
  326.  
  327.